package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Api("商家端订单管理相关接口")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {


    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }


    /**
     * 接单
     *
     * @param confirmDTO dto
     * @return 提示
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> confirm(@RequestBody OrdersConfirmDTO confirmDTO) {
        log.info("接单了:{}", confirmDTO);
        orderService.confirm(confirmDTO);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 订单搜索
     *
     * @param queryDTO dto
     * @return 分页数据
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult<OrderVO>> conditionSearch(OrdersPageQueryDTO queryDTO) {
        log.info("订单搜索:{}", queryDTO);
        return Result.success(orderService.conditionSearch(queryDTO), MessageConstant.SELECT_OK);
    }


    /**
     * 查询订单详情
     *
     * @param id 订单id
     * @return vo
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        return Result.success(orderService.detail(id), MessageConstant.SELECT_OK);
    }


    /**
     * 拒单
     *
     * @param rejectionDTO dto
     * @return 提示
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO rejectionDTO) throws Exception {
        log.info("拒单:{}", rejectionDTO);
        orderService.rejection(rejectionDTO);
        return Result.success(MessageConstant.OPERATE_OK);
    }

    /**
     * 取消订单
     *
     * @param cancelDTO dto
     * @return 提示
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancel(@RequestBody OrdersCancelDTO cancelDTO) throws Exception {
        log.info("取消订单:{}", cancelDTO);
        orderService.cancel(cancelDTO);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 派送订单
     *
     * @param id 订单ID
     * @return 提示
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("派送订单:{}", id);
        orderService.delivery(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }

    /**
     * 完成订单
     *
     * @param id 订单ID
     * @return 提示
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable Long id) {
        log.info("完成订单:{}", id);
        orderService.complete(id);
        return Result.success(MessageConstant.OPERATE_OK);
    }

    /**
     * 各个状态的订单数量统计
     * @return 各个状态的订单数量统计
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");
        return Result.success(orderService.statistics(), MessageConstant.SELECT_OK);
    }
}
